package com.qzsf.backend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qzsf.backend.common.Result;
import com.qzsf.backend.dto.OrganizerDetailDTO;
import com.qzsf.backend.entity.OrganizerProfile;
import com.qzsf.backend.entity.UserAuth;
import com.qzsf.backend.mapper.OrganizerProfileMapper;
import com.qzsf.backend.mapper.UserAuthMapper;
import com.qzsf.backend.service.OrganizerService;
import com.qzsf.backend.util.Sha256Util;
import com.qzsf.backend.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/11/13 15:34 周三
 * @Email 1429278462@qq.com
 * @Remark 组织服务层实现类
 * @Version 1.0
 */
@Service
public class OrganizerServiceImpl implements OrganizerService {
    @Autowired
    private OrganizerProfileMapper organizerProfileMapper;
    @Autowired
    private UserAuthMapper userAuthMapper;

    @Override
    public Result getOrganizerInfoDetail(String name,int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<OrganizerDetailDTO> userDetailDtoList = organizerProfileMapper.getOrganizerDetail(name);
        if(userDetailDtoList == null){
            return Result.error("查询数据为空");
        }else{
            return Result.success(new PageInfo<>(userDetailDtoList));
        }
    }

    @Override
    public Result createOrganizer(OrganizerDetailDTO organizerDetailDto) {
        UserAuth userAuth = new UserAuth(organizerDetailDto);
        String sha256String = Sha256Util.getSHA256String(userAuth.getPassword());
        userAuth.setPassword(sha256String);
        userAuthMapper.createUserAuth(userAuth);
        int id = userAuthMapper.findByUsername(userAuth.getUsername()).getId();
        OrganizerProfile organizerProfile = new OrganizerProfile(id,organizerDetailDto);
        if(organizerProfileMapper.createOrganizer(organizerProfile)>0){
            return Result.success();
        }else {
            return Result.error("创建异常");
        }
    }


    @Override
    public Result updateOrganizer(OrganizerProfile organizerProfile) {
        if(organizerProfileMapper.updateOrganizer(organizerProfile)>0){
            return Result.success();
        }else{
            return Result.error("修改异常");
        }
    }

    @Override
    public Result getOrganizerProfileById(int id) {
        OrganizerProfile organizerProfile = organizerProfileMapper.getOrganizerProfileById(id);
        if(organizerProfile == null){
            return Result.error("没有此条记录");
        }else{
            return Result.success(organizerProfile);
        }
    }

    @Override
    public Result getOrganizerBasicProfile() {
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        return Result.success(organizerProfileMapper.getOrganizerBasicInfo(id));
    }

    @Override
    public Result getAllOrganizerProfileMobile() {
        return Result.success(organizerProfileMapper.getAllOrganizerProfileMobile());
    }
}
